#if UNITY_EDITOR
using Net.Unity;
using Net.UnityComponent;
using System;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(NetworkSceneManager), editorForChildClasses: true)]
[CanEditMultipleObjects]
public class NetworkSceneManagerEdit : Editor
{
    private NetworkSceneManager nsm;

    private void OnEnable()
    {
        nsm = target as NetworkSceneManager;
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        EditorGUI.BeginChangeCheck();
        var rect = EditorGUILayout.GetControlRect();
        DragAndDropHandler(rect);
        nsm.collection = EditorGUI.ObjectField(rect, new GUIContent("Collection", "网络物体预制体收集器, 在Assets右键菜单选项GameDesigner/UnityNetworkComponent/PrefabCollection创建"), nsm.collection, typeof(IPrefabCollection), true);
        if (nsm.collection == null)
        {
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("创建预制体收集器"))
            {
                nsm.collection = PrefabCollection.CreatePrefabCollection();
            }
            if (GUILayout.Button("添加预制体收集器组件"))
            {
                nsm.collection = nsm.gameObject.AddComponent<PrefabCollectionComponent>();
            }
            EditorGUILayout.EndHorizontal();
        }
        if (EditorGUI.EndChangeCheck())
            EditorUtility.SetDirty(nsm);
    }

    private void DragAndDropHandler(Rect rect)
    {
        if (rect.Contains(Event.current.mousePosition))
        {
            if (Event.current.type == EventType.DragUpdated)
            {
                var isValid = false;
                foreach (var obj in DragAndDrop.objectReferences)
                {
                    if (obj is IPrefabCollection)
                    {
                        isValid = true;
                        break;
                    }
                    if (obj is not GameObject go)
                        continue;
                    var components = go.GetComponents<Component>();
                    foreach (var component in components)
                    {
                        if (component is IPrefabCollection)
                        {
                            isValid = true;
                            break;
                        }
                    }
                }
                DragAndDrop.visualMode = isValid ? DragAndDropVisualMode.Link : DragAndDropVisualMode.Rejected;
            }
            else if (Event.current.type == EventType.DragPerform)
            {
                foreach (var obj in DragAndDrop.objectReferences)
                {
                    if (obj is IPrefabCollection)
                    {
                        nsm.collection = obj;
                        GUI.changed = true;
                        break;
                    }
                    if (obj is not GameObject go)
                        continue;
                    var components = go.GetComponents<Component>();
                    for (int i = 0; i < components.Length; i++)
                    {
                        if (components[i] is IPrefabCollection)
                        {
                            nsm.collection = components[i];
                            GUI.changed = true;
                            break;
                        }
                    }
                }
            }
        }
    }
}
#endif